package com.lagou.edu.controller;

import com.lagou.edu.pojo.AuthCode;
import com.lagou.edu.service.AuthCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Heweipo on 2021/2/15.
 */
@RequestMapping("/code")
@RestController
@Slf4j
public class AuthCodeController {

    @Autowired
    private AuthCodeService authCodeService;

    @GetMapping("/create/{email}")
    public boolean createCode(@PathVariable("email") String email){
        log.info("请求生成验证码，email：{}",email);

        AuthCode authCode = new AuthCode();
        authCode.setEmail(email);
        AuthCode authCode1 = authCodeService.create(authCode);
        if(authCode1 != null){
            log.info("email:{} 的验证码生成成功，",email,authCode1);
            return true;
        }

        return false;
    }

    @GetMapping("/validate/{email}/{code}")
    public Integer validate(@PathVariable("email") String email, @PathVariable("code") String code){
        log.info("请求验证验证码，email：{}，code：{}",email,code);

        return authCodeService.validate(email,code);
    }

}
